<?php
/**
 * Created by PhpStorm.
 * User: xiaom
 * Date: 2016/10/30
 * Time: 1:02
 */

namespace Api\Controller;

use Think\Controller;

class WeChatController extends Controller
{
    //获取token openid 等信息
    public function getOpenId()
    {
        if (IS_POST) {
            vendor('Requests.library.Requests');
            \Requests::register_autoloader();
            $url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
            $headers = array();
            $data = array(
                'appid' => C('WeChat.APPID'),
                'secret' => C('WeChat.SECRET'),
                'code' => I('post.code'),
                'grant_type' => 'authorization_code',
            );
            $response = \Requests::post($url, $headers, $data);
            if ($response->success) {
                $this->ajax(200, '获取成功', $response->body);
            } else {
                $this->ajax(404, '获取失败');
            }
        } else {
            $this->ajax(500, '非法操作');
        }
    }

    //刷新token
    public function refresh_token()
    {
        if (IS_POST) {
            vendor('Requests.library.Requests');
            \Requests::register_autoloader();
            $url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';
            $headers = array();
            $data = array(
                'appid' => C('WeChat.APPID'),
                'grant_type' => 'refresh_token',
                'refresh_token' => I('post.refresh_token'),
            );
            $response = \Requests::post($url, $headers, $data);
            if ($response->success) {
                $this->ajax(200, '获取成功', $response->body);
            } else {
                $this->ajax(404, '获取失败');
            }
        } else {
            $this->ajax(500, '非法操作');
        }
    }

    //获取用户信息
    public function getUserInfo()
    {
        if (IS_POST) {
            vendor('Requests.library.Requests');
            \Requests::register_autoloader();
            $url = 'https://api.weixin.qq.com/sns/userinfo';
            $headers = array();
            $data = array(
                'access_token' => I('post.access_token'),
                'openid' => I('post.openid'),
                'lang' => 'zh_CN'
            );
            $response = \Requests::post($url, $headers, $data);
            if ($response->success) {
                $this->ajax(200, '获取成功', $response->body);
            } else {
                $this->ajax(404, '获取失败');
            }
        } else {
            $this->ajax(500, '非法操作');
        }
    }


//    获取校区
    public function getCampus()
    {
        $model = M('campus');
        $result = $model->where(array('status' => 0))->select();
        if ($result) {
            $this->ajax(200, '获取校区成功', $result);
        } else {
            $this->ajax(400, '获取校区失败');
        }
    }

//    根据校区获取区域
    public function getBuilding()
    {
        if (IS_POST) {
            $campus_id = I('post.campus_id');
            $model = M('building');
            $result = $model->where(array('campus_id' => $campus_id, 'status' => 0))->select();
            if ($result) {
                $this->ajax(200, '获取区域成功', $result);
            } else {
                $this->ajax(400, '获取区域失败');
            }
        }
    }

    //获取运营商
    public function getOperator()
    {
        $model = M('operator');
        $result = $model->where(array('status' => 0))->select();
        if ($result) {
            $this->ajax(200, '获取运营商成功', $result);
        } else {
            $this->ajax(400, '获取运营商失败');
        }
    }

    //获取故障类型
    public function getFaultType()
    {
        $model = M('fault_type');
        $result = $model->where(array('status' => 0))->select();
        if ($result) {
            $this->ajax(200, '获取故障类型成功', $result);
        } else {
            $this->ajax(400, '获取故障类型失败');
        }
    }

    //保存保修单
    public function saveNet()
    {
        if (IS_POST) {
            $model = D('FailuresRepair');

            $where['open_id'] = I('post.open_id');

            $last_time = $model->where($where)->order('creation_time desc')->getField('creation_time');

            if (time() - $last_time > 1800) {
                if ($model->create()) {
                    $result = $model->add();
                    if ($result) {
                        $this->ajax(200, '提交成功');
                    } else {
                        $this->ajax(400, '提交失败');
                    }
                }
            } else {
                $this->ajax(500, '你刚报修过了，30分钟之后再报吧！');
            }
        }
    }

    //获取用户的报修列表
    public function getUserRepairList(){
        $model=D('FailuresRepair');
        $result = $model->getSimpleList();
        if ($result) {
            $this->ajax(200, '获取用户报修列表成功', $result);
        } else {
            $this->ajax(400, '获取用户报修列表失败');
        }
    }

    //获取用户某报修详细信息
    public function getUserRepairDetails(){
        if (IS_POST){
            $model=D('FailuresRepair');
            $id=I('post.id');
            $result=$model->where(array('id'=>$id))->find();
            if ($result) {
                $this->ajax(200, '获取报修信息成功', $result);
            } else {
                $this->ajax(400, '获取报修信息失败');
            }
        }else {
            $this->ajax(500, '非法操作');
        }
    }


    //ajax tool
    function ajax($code, $message, $data = array())
    {
        $this->ajaxReturn(array('code' => $code, 'message' => $message, 'data' => $data));
    }

}